import { GameComponent } from "@sapi-game/main";
import { RandomUtils, Vector3Utils } from "@sapi-game/utils";
import { HideAndSeekMainState } from "../mainState";
import { HideAndSeekPlayer, HideAndSeekRole } from "../player";

export class HideAndSeekEffectActuator extends GameComponent<HideAndSeekMainState> {
    override onAttach(): void {}

    locate(p: HideAndSeekPlayer) {
        if (p.role == HideAndSeekRole.Seeker) {
            const hiders = this.context.players.filter(
                (p) => p.alive && p.role == HideAndSeekRole.Hider
            );
            const random = RandomUtils.choice(hiders);
            if (!random?.player) return p.sendMessage("未定位到躲藏者");
            p.sendMessage(
                `§v定位到一个躲藏者:§e${
                    random?.name
                } §q坐标:${Vector3Utils.toString(random.player.location)}`
            );
        }
    }

    tp(p: HideAndSeekPlayer) {
        if (!p.player) return;
        const hiders = this.context.players.filter(
            (p) => p.alive && p.role == HideAndSeekRole.Hider && p.isValid
        );
        const target = RandomUtils.choice(hiders);
        if (!target?.player) return p.sendMessage("无法获取躲藏者");
        p.sendMessage(`你即将传送到:§e${target.name}§r的位置`);
        target.sendMessage("躲藏者即将传送到你的当前位置");
        const loc = target.player.location;
        //等待
        this.runner.runDelay(() => {
            p.player?.teleport(loc);
        }, 40);
    }
}
